package io.github.bigbird0101.yousishi.sql.print;

import com.alibaba.druid.sql.SQLUtils;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;

/**
 * @author m15179835326@163.com
 * @date 2023-04-06 10:55
 */
public abstract class AbstractPrintSql implements PrintSql{
    public CtMethod getPrintSqlMethod(CtClass cls) throws Exception {
        CtMethod originMethod = ClassPool.getDefault()
                .getMethod(AbstractPrintSql.class.getName(), "dataPrintSql");
        CtMethod method = CtNewMethod.copy(originMethod, cls, null);
        method.setName("davaPrintSql");
        return method;
    }

    public void dataPrintSql(String sql, boolean isFormatSql) {
        SQLUtils.FormatOption option = new SQLUtils.FormatOption();
        option.setPrettyFormat(isFormatSql);
        System.err.println(SQLUtils.formatPGSql(sql, option));
    }
}
